package com.xo.mall.server.impl;

import com.xo.common.Result.ResultResponse;
import com.xo.common.constant.NumberConstant;
import com.xo.common.enums.CategoryLevelEnum;
import com.xo.common.enums.ResultEnum;
import com.xo.common.pojo.dto.CreateCategoryDto;
import com.xo.common.pojo.entity.PmsCategoryEntity;
import com.xo.mall.convert.PmsCategoryConvert;
import com.xo.mall.mapper.PmsCategoryMapper;
import com.xo.mall.server.PmsCategoryService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class PmsCategoryServiceImpl implements PmsCategoryService {

    @Resource
    private PmsCategoryMapper pmsCategoryMapper;

    @Override
    public ResultResponse<Object> createCategory(CreateCategoryDto createCategoryDto) {
        //如果是一级分类，则父级id设置为0
        if (Objects.equals(CategoryLevelEnum.LEVEL_1.getCode(), createCategoryDto.getCategoryLevel())) {
            createCategoryDto.setCategoryLevel(NumberConstant.INT_ZERO);
        }
        PmsCategoryEntity pmsCategoryEntity = PmsCategoryConvert.INSTANCE.CreateCategoryDtoToPmsCategoryEntity(createCategoryDto);
        int count = pmsCategoryMapper.createCategory(pmsCategoryEntity);
        if (count > NumberConstant.INT_ZERO) {
            return ResultResponse.success(ResultEnum.CREATE_SUCCESS);
        }
        return ResultResponse.fail(ResultEnum.CREATE_FAIL);
    }
}
